<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账单日期" prop="date">
        <el-date-picker
          v-model="dateRange"
          type="monthrange"
          value-format="yyyy-MM"
          range-separator="至"
          start-placeholder="开始月份"
          end-placeholder="结束月份"

          style="width: 240px"
          @keyup.enter.native="handleQuery">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="文件名称" prop="excelName">
        <el-input
          v-model="queryParams.excelName"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账单号" prop="storeCode">
        <el-input
          v-model="queryParams.storeCode"
          placeholder="请输入账单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" ref="toolbar">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-upload2"
          @click="handleImport"
          v-hasPermi="['business:payExcels:import']"
        >账单导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-tooltip placement="top">
          <div slot="content">支持选中下载和查询条件下载<br/>若同时选中和查询则合并下载</div>
          <el-button
            type="primary"
            plain
            :disabled="multiple && !hasQueryParams"
            icon="el-icon-download"
            @click="handleExport('','batchDownload')"
            v-hasPermi="['business:payExcels:batchExport']"
          >汇总下载</el-button>
        </el-tooltip>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          :disabled="multiple"
          icon="el-icon-delete"
          @click="handleDelete"
          v-hasPermi="['business:payExcels:remove']"
        >批量删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div ref="divTable" :style="'height:' + divHeight + 'px'">
      <el-table v-loading="loading" :data="aliPayExcelsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" :selectable="(row) => row.status === '4'"/>
        <el-table-column label="操作" align="center" width="250">
          <template slot-scope="scope">
            <el-tooltip content="请等待账单上传完成或账单上传失败" placement="top" :disabled="scope.row.status !== '2' && scope.row.status !== '1'">
              <el-button
                type="text"
                icon="el-icon-view"
                @click="handleViewDetails(scope.row)"
                :disabled="scope.row.status === '2' || scope.row.status === '1'"
                v-hasPermi="['business:payExcels:view']"
              >查看</el-button>
            </el-tooltip>
            <el-tooltip content="请等待账单生成完成" placement="top" :disabled="scope.row.status === '4'">
              <el-button
                type="text"
                icon="el-icon-download"
                @click="handleExport(scope.row)"
                :disabled="scope.row.status !== '4'"
                v-hasPermi="['business:payExcels:export']"
              >下载</el-button>
            </el-tooltip>
            <el-tooltip content="请等待账单生成完成" placement="top" :disabled="scope.row.status === '4'">
              <el-button
                type="text"
                icon="el-icon-refresh"
                @click="handleGenerate(scope.row)"
                :disabled="scope.row.status !== '4'"
                v-hasPermi="['business:payExcels:gen']"
              >重新生成</el-button>
            </el-tooltip>
            <el-button
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['business:payExcels:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
        <el-table-column label="账单日期" align="center" prop="billDate" />
        <el-table-column label="文件名称" align="center" prop="excelName" show-overflow-tooltip/>
        <el-table-column label="账单号" align="center" prop="storeCode" />
        <el-table-column label="店铺名称" align="center" prop="storeName" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="上传人" align="center" prop="userId" width="100">
          <template v-slot="scope">
            <user-tag :value="scope.row.userId" type="userId"/>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="80">
          <template v-slot="scope">
            <el-tag v-if="scope.row.status === '1'" type="warning">上传中</el-tag>
            <el-tag v-if="scope.row.status === '2'" type="danger">上传失败</el-tag>
            <el-tag v-if="scope.row.status === '3'" type="warning">生成中</el-tag>
            <el-tag v-if="scope.row.status === '4'" type="success">生成成功</el-tag>
            <el-tag v-if="scope.row.status === '5'" type="danger">生成失败</el-tag>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :pageRefs="this.$refs"
          :total="total"
          :pageSizes= [20,50,100,150]
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
      />
    </div>

    <!-- 云上传对话框 -->
    <upload-dialog ref="uploadDialog" :visible.sync="uploadVisible" @close="handleUploadDialogClose"  @refreshTable="refresh"/>

    <!-- 查看明细 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <div :style="'height: calc(70vh - 120px)'">
        <el-table v-loading="dialogLoading" :data="detailsList" height="94%">
          <el-table-column label="序号" align="center" width="100">
            <template v-slot="scope">
              {{ (dialogQueryParams.pageNum - 1) * dialogQueryParams.pageSize + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="业务类型" align="center" prop="businessType" width="150"/>
          <el-table-column label="备注" align="left" prop="businessTypeRemark" show-overflow-tooltip/>
          <el-table-column label="收入金额" align="center" prop="incomeAmount" width="100"/>
          <el-table-column label="支出金额" align="center" prop="expenditureAmount" width="100"/>
        </el-table>
        <pagination
          style="bottom: 5px"
          v-show="dialogTotal>0"
          :total="dialogTotal"
          :page.sync="dialogQueryParams.pageNum"
          :limit.sync="dialogQueryParams.pageSize"
          @pagination="getDetailsList"
        />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPayExcels, delPayExcels, getPayDetails, reGenerate, exportExcel } from "@/api/business/payExcels";
import ResizeMixin from "@/layout/mixin/ResizeHandler";
import uploadDialog from "./uploadDialog.vue"

export default {
  name: "AliPayExcels",
  dicts: ["money_rule_platform"],
  components: { uploadDialog },
  mixins: [ResizeMixin],
  data() {
    return {
      // div高度
      divHeight: 0,
      // 遮罩层
      loading: true,
      // 弹出框表格遮罩层
      dialogLoading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 账单明细表格数据
      aliPayExcelsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        rulePlatform: 1,
        excelName: null,
        storeCode: null,
        storeName: null,
        userId: null,
        deptId: null,
      },
      // 上传对话框
      uploadVisible: false,
      // 明细列表
      detailsList: [],
      // 弹出框条数
      dialogTotal: 0,
      // 弹出框查询参数
      dialogQueryParams: {
        pageNum: 1,
        pageSize: 20,
      },
      // 日期范围
      dateRange: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询账单明细列表 */
    getList() {
      this.loading = true;
      listPayExcels(this.$addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.aliPayExcelsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('注意：此删除不可恢复，是否确认删除？').then(function() {
        return delPayExcels(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 刷新方法 */
    refresh() {
      this.getList();
    },
    /** 账单导入 */
    handleImport(){
      this.uploadVisible = true;
    },
    /** 上传弹出框关闭 */
    handleUploadDialogClose(){
      this.uploadVisible = false;
    },
    /** 查看明细 */
    handleViewDetails(row){
      this.title = row.excelName;
      this.open = true;
      this.dialogQueryParams = {
        pageNum: 1,
        pageSize: 20,
        mainId: row.id
      };
      this.getDetailsList();

    },
    /** 账单明细列表 */
    getDetailsList(){
      this.dialogLoading = true;
      getPayDetails(this.dialogQueryParams).then(response => {
        this.detailsList = response.rows;
        this.dialogTotal = response.total;
        this.dialogLoading = false;
      });
    },
    /** 重新生成 */
    handleGenerate(row){
      reGenerate(row)
    },
    /** 账单导出 */
    handleExport(row, type) {
      if (type === 'batchDownload') {
        this.queryParams.id = null;
        this.queryParams.ids = this.ids;
      } else {
        this.queryParams.ids = null;
        this.queryParams.id = row.id;
      }
      exportExcel(this.queryParams)
    }
  },
  computed: {
    hasQueryParams() {
      return (
        this.dateRange?.length > 0 ||
        this.queryParams.excelName ||
        this.queryParams.storeCode ||
        this.queryParams.storeName
      )
    }
  }

};
</script>
